<?php

class DebugTools
{
    static $bLimitTooLong = false;
    static $bLimitLen = 150;
    static function bll( $bll, $len=150 )
    {
        self::$bLimitTooLong = !!$bll;
        self::$bLimitLen = (int)$len;
    }

    static function vp($obj)
    {
        if($obj)
            print_r($obj) ;
        else
            var_dump($obj);
    }

    static function lc( &$info )
    {
        if( self::$bLimitTooLong
            && count($info) > self::$bLimitLen )
        {
            $info = "string count(".count($info).")";
        }
    }

    static function pk($arr, $keys=null)
    {
        if($keys == null)
        {
            $keys = array_keys(end($arr));
        }

        foreach($arr as $i=>$one)
        {
            echo "{$i},";
            foreach($keys as $key)
            {
                self::lc( $one[$key] ) ;
                echo $key."=".$one[$key].",\t";
            }
            echo "\n";
        }
    }

    static function pkk($arr, $keys=null)
    {
        if($keys == null)
        {
            $keys = array_keys(end(end($arr)));
        }

        foreach($arr as $k=>$kone)
        {
            if($kone)
            {
                foreach($kone as $i=>$ione)
                {
                    echo "{$k},{$i},";
                    foreach($keys as $key)
                    {
                        self::lc( $ione[$key] ) ;
                        echo $key."=".$ione[$key].",\t";
                    }
                    echo "\n";
                }
            }
            else
            {
                echo "{$k},\n";
            }
        }
    }

    static function pc($arr)
    {
         foreach($arr as $key=>$index)
         {
             echo $key."\t".count($index)."\n";
         }
    }

    static $dir = '/usr/local/board/server/cache_tools/';
    static $pre = 'pa_';

    static function fileName( $key )
    {
        return self::$dir.self::$pre.$key.'.txt';
    }

    static function outOne( $arr )
    {
        static $bw = false;
        if( !$bw )
        {
            foreach( $arr as $key => $one )
            {
                if( count($one) < 4 )
                {
                    return false;
                }
            }

            foreach( $arr as $key => $one )
            {
                file_put_contents(self::fileName($key), json_encode($one));
            }

            $bw = true;
        }
    }

    static function inOne( &$arr )
    {
        foreach( $arr as $key => &$one )
        {
            $str = file_get_contents(self::fileName($key));
            $one = json_decode($str, true);
        }
    }
}


if($argv[0] == basename(__FILE__))
{
    $keys = range(0,2);
    foreach($keys as $key)
    {
        $out[$key] = array(
                    __LINE__, 
                    __LINE__, 
                    __LINE__, 
                    __LINE__, 
                    rand(0, 100),
                );
        $in[$key] = '';
    }

    print_r($out);
    DebugTools::outOne($out);
    DebugTools::inOne($in);
    print_r($in);
}
